package com.partsoft.spring.support;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.partsoft.spring.log.Log;
import com.partsoft.utils.Assert;
import com.partsoft.utils.StringUtils;

public class DitsPropertyConfigurerBeanDefinitionParaser implements BeanDefinitionParser {

	public static final String ElementName = "context-config";

	public static final String DefaultDataSourceFactoryID = "dataSourceFactory";

	public BeanDefinition parse(Element element, ParserContext parserContext) {
		Assert.notNull(element);
		Assert.notNull(parserContext);
		String config_location = element.hasAttribute("location") ? element.getAttribute("location") : null;
		BeanNameGenerator beanNameGenerator = parserContext.getReaderContext().getReader().getBeanNameGenerator();
		BeanDefinitionRegistry beanDefinitionRegistry = parserContext.getRegistry();

		List<String> location_list = new LinkedList<String>();
		if (StringUtils.hasText(config_location))
			location_list.add(config_location);

		for (int j = 0; j < element.getChildNodes().getLength(); j++) {
			Node locations_node = element.getChildNodes().item(j);
			String locations_nodename = locations_node.getNodeName();
			locations_nodename = locations_nodename.indexOf(":") >= 0 ? locations_nodename.substring(locations_nodename
					.indexOf(":") + 1) : locations_nodename;
			if (locations_node instanceof Element && "locations".equals(locations_nodename)
					&& locations_node.getChildNodes().getLength() > 0) {
				for (int i = 0; i < locations_node.getChildNodes().getLength(); i++) {
					Node location_node = locations_node.getChildNodes().item(i);
					String location_nodename = location_node.getNodeName();
					location_nodename = location_nodename.indexOf(":") >= 0 ? location_nodename.substring(location_nodename
							.indexOf(":") + 1) : location_nodename;
					if (location_node instanceof Element && "location".equals(location_nodename)
							&& ((Element) location_node).hasAttribute("value")) {
						if (StringUtils.hasText(((Element) location_node).getAttribute("value"))) {
							location_list.add(((Element) location_node).getAttribute("value"));
						}
					}
				}
			}
		}

		BeanDefinitionBuilder propertyConfigurerBuilder = BeanDefinitionBuilder
				.rootBeanDefinition(DitsPropertyConfigurer.class);

		propertyConfigurerBuilder.addPropertyValue("locations", location_list);

		if (Log.isDebugEnabled()) {
			Log.debug(propertyConfigurerBuilder.toString());
		}
		BeanDefinition propertyConfigurerBean = propertyConfigurerBuilder.getBeanDefinition();
		beanDefinitionRegistry.registerBeanDefinition(
				beanNameGenerator.generateBeanName(propertyConfigurerBean, beanDefinitionRegistry),
				propertyConfigurerBean);
		return null;
	}

}
